<script language="javascript">
	function funLocDS(SID){
		window.location='?mod=tinnhan&SID=' + SID;
	}
</script>
<?
if(isset($_GET['method']))
{
	$method = $_GET['method'];
	if($method=="del"){
		$sListId="";
		if(isset($_POST["chon"]))$sListId=$_POST["chon"];
		$sql="delete from ".$prefix."tinnhan where tn_id in('".str_replace(",","','",$sListId)."')";
		mysql_query($sql);
		Redirect("Bạn đã xoá thành công","?mod=tinnhan");
	}else if($method=="delete"){
		$IdList=$_GET["id"];
		$sql="delete from ".$prefix."tinnhan where tn_id=$IdList";
		mysql_query($sql);
		Redirect("Đã xóa thành công","?mod=tinnhan");		
	}
}

	$strSQL = "select * from ".$prefix."tinnhan WHERE 1";
	
	
	if(isset($_GET['nguoigui']) && ($_GET['nguoigui'] != "") ){
		$nguoigui = intval($_GET['nguoigui']);
		$strSQL .= " AND tn_nguoigui = '".$nguoigui."'";
	}
	
	if(isset($_GET['nguoinhan']) && ($_GET['nguoinhan'] != "") ){
		$nguoinhan = intval($_GET['nguoinhan']);
		$strSQL .= " AND tn_nguoinhan = '".$nguoinhan."'";
	}
	
	
	if(isset($_GET['SID']) && ($_GET['SID'] != 3)){
		$SID = intval($_GET['SID']);
		if($SID == 0){
			$strSQL .= " ORDER BY tn_id ASC";
		}else{
			$strSQL .= " ORDER BY tn_id DESC";
		}
	}else{$SID = 3;}
	
	
	///////Paging
	
	$pp=10; 
	$parr=array();
	$parr=paging($strSQL,$pp);
	$page=$parr[0];
	$limitvalue=$parr[1];
	$numofpages=$parr[2];
	$strSQL.=" LIMIT $limitvalue, $pp";
	$rs = mysql_query($strSQL) or die('Not query');
	$i=0;
	$isexits=false;
	if(mysql_num_rows($rs)) $isexits=true;
?>
<script language="javascript">
	function funSearch(){
		var a = document.getElementById('nguoigui').value;
		var b = document.getElementById('nguoinhan').value;
		window.location = '?mod=tinnhan&nguoigui=' + a + '&nguoinhan=' + b;
	}
</script>
<div class="newsList-path"></div>
<div style="border:1px dotted #FFCC00; padding:10px;">
	<div>Tìm theo ID người gửi : &nbsp;&nbsp;<input type="text" name="nguoigui" id="nguoigui" size="15"></div>
	<br />
	<div>Tìm theo ID người nhận : <input type="text" name="nguoinhan" id="nguoinhan" size="15"></div>
	<div style="padding-left:120px; padding-top:5px;"><input type="submit" name="submit" value="Tìm" onclick="funSearch();"/></div>
</div>
	<br />
	<div style="margin-bottom:5px; font-size:14px;">
	 Có <?=$db->num_rows($rs)?>
	  tin nhắn được tìm thấy </div>
	<div align="right" class="newsList-search-up">&nbsp;
	  &nbsp;
	  <select name="FilterByStatus" id="SID" onchange="return(funLocDS(this.value));">
	<option value="3" <? if($SID == 3){?> selected="selected" <? } ?>>--- Lọc Danh Sách ---</option>
	<option value="1" <? if($SID == 1){?> selected="selected" <? } ?>>Mới dần</option>
	<option value="0" <? if($SID == 0){?> selected="selected" <? } ?>>Cũ dần</option>
	  </select>
</div>
	<form name="frmListSp" method="post" action="?mod=tinnhan&method=del" onSubmit="return checkInput(this,'Bạn hãy chọn ít nhất một giá trị','Bạn chắc chắn muốn xoá không');">
	<input type="hidden" name="chon" />
	<table width="100%" border="1" bordercolor="#B2DAEE" cellpadding="4">
		<tr class="newsList-content-head">
		<td width="4%" class="newsList-content-view"><input type="checkbox" name="chkall" value="0" <? if($isexits) echo "onClick=\"docheck(this.form,0);\""?>></td>
			<td width="18%" class="newsList-content-title">Người nhận </td>
          <td width="20%" class="newsList-content-view"><div align="center">Người gửi </div></td>
		  <td width="22%" class="newsList-content-view"><div align="center">Thời gian </div></td>
		  <td width="19%" class="newsList-content-view"><div align="center">Chi tiết </div></td>
		  <td width="17%" class="newsList-content-view"><div align="center">Tùy chọn </div></td>
		</tr>
				<?
				while($rows = mysql_fetch_array($rs))
				{
					$i++;
					$tn_id = $rows['tn_id'];
					$tn_nguoigui = $rows['tn_nguoigui'];
					$tn_nguoigui_ten = tenNguoiDung($tn_nguoigui);
					$tn_nguoinhan = $rows['tn_nguoinhan'];
					$tn_nguoinhan_ten = tenNguoiDung($tn_nguoinhan);
					$tn_thoigian = format_date_time($rows[tn_thoigian]);
				?>				
		<tr <? if($id == $tn_id ){?> bgcolor="#FEF2CB" <? } ?> onclick="?mod=tinnhan&act=xem&id=<?=$tn_id?>">
			<td class="newsList-content-title"><input type="checkbox" name="Xoa" value="<?=$tn_id?>" onClick="docheckone(this.form);"></td>
			<td class="newsList-content-view"><div align="center"><?=$tn_nguoigui_ten?></div>
			</td>
			<td class="newsList-content-view"><div align="center"><?=$tn_nguoinhan_ten?></div></td>
			<td class="newsList-content-view"><div align="center"><?=$tn_thoigian?></div></td>
		    <td class="newsList-content-view"><div align="center"><a class="newsList-link newsList-name" href="?mod=tinnhan&act=xem&id=<?=$tn_id?>"><b>Xem chi tiết</b></a></div></td>
	      <td class="newsList-content-view"><div align="center">
		  <a href="?mod=tinnhan&method=delete&id=<?=$tn_id?>" class="menu" onclick="return confirm('Bạn có chắc chắn muốn xóa không?')"><img src="images/delete_on.gif" border="0" /></a></div></td>
        </tr>
		<? }?>
	<tr>
   <td colspan="11" align="center">
   <input type="submit" name="submit" value="   Xoá   " class="Button">
   <input type="reset" value="Làm lại" class="Button">   </td>
   </tr>
</table>
	</form>
	<div class="newsList-pagging">
	<div class="raovat-paging-bound">
	<span style="padding:5px;">
	<p align="center" class="font">
			<?php echo page_div( "?mod=tinnhan&SID=".$SID."&p=%d_pg", "10", ceil ( $numofpages ), $page ); ?>
	</p>
	</span>
	</div>
	</div>
<?
	if(isset($_GET['act']) && ($_GET['act'] == 'xem') ){
		if(isset($_GET['id'])){$id = intval($_GET['id']);}else{die();}
		$rs = $db->query("SELECT * FROM ".$prefix."tinnhan WHERE tn_id = '".$id."'");
		$row = $db->fetch_array($rs);
		$tn_chitiet = $myString->unQuote(stripslashes($row[tn_chitiet]));
?>
	<b>-- Chi tiết -- </b> <br /><br />
	<div style="font-weight:bold; padding:10px; border:1px solid #FF9900;">
		<font color="#FF0000">ID : <?=$id?></font>
		<br />
		&nbsp;&nbsp;&nbsp;<?=$tn_chitiet?>
	</div>
<? } ?>
	<div class="newsList-button-back"><a href="javascript: history.back()">[Quay lại]</a></div>